package com.wly.ui
{
	import com.wly.ItemEditerMain;
	import com.wly.data.ItemVO;
	
	import fl.controls.UIScrollBar;
	import fl.events.ScrollEvent;
	
	import flash.display.Sprite;

	
	public class ItemGrid extends Sprite
	{
		private var _items:Array;
		private var _itemVoArr:Array;
		private var _selecttedIndex:int;
		public var itemEditerMain:ItemEditerMain;
		
		private const ViewWidth:int = 241;
		private const ViewHeight:int= 300;
		private const StartY:Number = 39.25;
		private const Capacity:int  = 6;
		
		private var _mcScrollBar:UIScrollBar;
		private var _mcContent:Sprite;
		private var _mcMask:Sprite;
		
		public function ItemGrid()
		{
			super();
			init();
		}
		
		private function init():void
		{
			_mcScrollBar = getChildByName("mcScrollBar") as UIScrollBar;

		}
		
		public function setItemList(items:Array):void
		{
			_itemVoArr = items;
			if(!_itemVoArr)
			{
				return;
			}
			
			if(_mcContent)
			{
				removeChild(_mcContent);
				_mcContent = null;
			}
			_mcMask = new Sprite();
			_mcMask.graphics.beginFill(0x000000, 0);
			_mcMask.graphics.drawRect(0,0,ViewWidth,ViewHeight);
			_mcMask.graphics.endFill();
			
			_mcContent = new Sprite();
			_mcContent.y = StartY;
			_mcContent.mask = _mcMask;
			_mcContent.addChild(_mcMask);
			addChild(_mcContent);
			_items = [];
			
			var i:int;
			var item:ItemVO;
			var itemSketchy:ItemSketchy;
			for(i=0; i<_itemVoArr.length; i++)
			{
				item = _itemVoArr[i] as ItemVO;
				itemSketchy  = new ItemSketchy();
				itemSketchy.updateItemInfo(item);
				itemSketchy.index = i;
				itemSketchy.itemGrid = this;
				itemSketchy.y = ItemSketchy.ITEMHEIGHT*i;
				_mcContent.addChild(itemSketchy);
				_items.push(itemSketchy);
			}
			setSelect(0);
			
			if(_items.length > Capacity)
			{
				enbleScroll();
			}
			else
			{
				disableScroll();
			}
		}
	
		private function enbleScroll():void
		{
			_mcScrollBar.visible = true;
			_mcScrollBar.setScrollProperties(ItemSketchy.ITEMHEIGHT, 0, (_items.length-Capacity)*ItemSketchy.ITEMHEIGHT);
			_mcScrollBar.addEventListener(ScrollEvent.SCROLL, onScroll);
		}
		
		private function disableScroll():void
		{
			_mcScrollBar.visible = false;
			_mcScrollBar.removeEventListener(ScrollEvent.SCROLL, onScroll);
		}
		
		private function onScroll(e:ScrollEvent):void
		{
			var i:int;
			var itemSketchy:ItemSketchy;
			for(i=0; i<_items.length; i++)
			{
				itemSketchy = _items[i] as ItemSketchy;
				if(itemSketchy)
				{
					itemSketchy.y = ItemSketchy.ITEMHEIGHT*i-e.position;
				}
			}
		}
		
		public function setSelect(val:int):void
		{
			var itemSketchy:ItemSketchy;
			
			itemSketchy = _items[_selecttedIndex] as ItemSketchy;
			if(itemSketchy)
			{
				itemSketchy.selected = false;
			}
			_selecttedIndex = val;
			itemSketchy = _items[_selecttedIndex] as ItemSketchy;
			if(itemSketchy)
			{
				itemSketchy.selected = true;
			}
			if(itemEditerMain)
			{
				itemEditerMain.onItemSelectChg(_selecttedIndex);
			}
		}
		
		public function getItemVoByIndex(index:int):ItemVO
		{
			if(!_itemVoArr)
			{
				return null;
			}
			
			return _itemVoArr[index] as ItemVO;
		}
	}
}